var app = getApp()
Page({
  data: {
    userName:"",
    telNumber: "",
    provinceName: "",
    cityName: "",
    countyName: "",
    detailInfo: "",
    postalCode:'',
    nationalCode:'',
    selectAddressText:'选择新的收货地址',
    remark:''
  },
  choseAddressIn:function (event) {
    var that = this;
    var index=event.currentTarget.dataset.index;
    var addressList = that.data.addressList;
    if(addressList!=null){
      for(var i=0;i<addressList.length;i++){
        if(i==index){
          addressList[i].selected=true;
          that.setData(addressList[i]);
        }else{
          addressList[i].selected=false;
        }
      }
       that.setData({
          addressList:addressList
        });
    }
  },
  choseAddress:function (event) {
    var that = this

    wx.chooseAddress({
      success: function (res) {
        var addressList = that.data.addressList;
        if(addressList==null){
          addressList=[];
        }
        addressList.unshift(res);
        for(var i=0;i<addressList.length;i++){
          if(i==0){
            addressList[i].selected=true;
            that.setData(addressList[i]);
          }else{
            addressList[i].selected=false;
          }
        }
        that.setData({
          addressList:addressList,
          remark:''
        });
      }
    })
  },
  pay:function(event){
      var that = this

      var formId = event.detail.formId;

      if(that.data.userName=='')
      {
        wx.showToast({
            title: '地址信息不完整',
            duration: 2000
        })
        return;
      }

      if(that.data.telNumber=='')
      {
        wx.showToast({
            title: '地址信息不完整',
            duration: 2000
        })
        return;
      }

      if(that.data.detailInfo=='')
      {
        wx.showToast({
            title: '地址信息不完整',
            duration: 2000
        })
        return;
      }

      var payData = wx.getStorageSync('payData');
      for (var key in that.data) {
        payData[key]=that.data[key];
      }

        wx.showLoading({
          title: '下单中',
          mask:true
        })

        wx.request({
          url: app.globalData.serverUrl+'/app/mp', 
          method:'post',
          dataType:'json',
          data: {
            sessionId:wx.getStorageSync('pi_wxapp_sessionId'),
            messageId: 'wxPayUnifyOrder',
            body: payData
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
             wx.hideLoading()
            if(res.data.success){

              var orderNo = res.data.orderNo;

              wx.requestPayment({
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr,
                'package': res.data.packageStr,
                'signType': res.data.signType,
                'paySign': res.data.sign,
                'success':function(res){

                  wx.request({
                    url: app.globalData.serverUrl + '/app/mp',
                    method: 'post',
                    dataType: 'json',
                    data: {
                      messageId: 'sendWxTemplate',
                      body: {
                        formId: formId,
                        orderNo:orderNo
                      }
                    },
                    header: {
                      'content-type': 'application/json'
                    },
                    success: function (res) {
                      if (res.data.success) {
                        console.log('发送模板消息成功');
                      }
                    }
                  })

                  wx.redirectTo({
                    url: '../success/success?status=0'
                  })  
                },
                'fail':function(res){
                  if(res.errMsg=='requestPayment:fail cancel'){
                    wx.showToast({
                      title: '没有支付',
                      image:'../../images/X.png',
                      duration: 2000
                    })


                    wx.request({
                      url: app.globalData.serverUrl + '/app/mp',
                      method: 'post',
                      dataType: 'json',
                      data: {
                        messageId: 'wxPayCancel',
                        body: {
                          orderNo: orderNo
                        }
                      },
                      header: {
                        'content-type': 'application/json'
                      }
                    })


                  }else{

                    wx.redirectTo({
                      url: '../success/success?status=1'
                    })
                  }
                }
              })
            }else{
              wx.showToast({
                title: res.data.message,
                duration: 2000
              })
            }
          }
        })
      
  },
  onLoad: function (object) {
    var that = this
    var sessionId = wx.getStorageSync('pi_wxapp_sessionId')
    wx.request({
          url: app.globalData.serverUrl+'/app/mp', 
          method:'post',
          dataType:'json',
          data: {
            sessionId:sessionId,
            messageId: 'queryUserAddress',
            body: {
            }
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
           if(res.data.success){
             if(res.data.addressList==null||res.data.addressList.length==0){
               that.setData({
                  remark:'您还没有输入收货地址',
                  selectAddressText:'创建收货地址'
               });
             }

             if(res.data.addressList!=null){
               for(var i=0;i<res.data.addressList.length;i++){
                 for(var i=0;i<res.data.addressList.length;i++){
                  if(i==0){
                    res.data.addressList[i].selected=true;
                    that.setData(res.data.addressList[i]);
                  }else{
                    res.data.addressList[i].selected=false;
                  }
                }
               }
             }

             that.setData({
               addressList:res.data.addressList
             });
            //  that.setData(res.data);
            //  if(res.data.detailInfo==null||res.data.detailInfo==''){
            //    that.setData({
            //      provinceName: "",
            //      cityName: "",
            //      countyName: "",
            //      detailInfo:'',
            //      remark:'您还没有输入收货地址',
            //      selectAddressText:'创建收货地址'
            //    });
            //  }
           }
          }
        })
  }
})